/*
 * pit.c
 *
 *  Created on: Feb 22, 2013
 *      Author: btgarber
 */

#include "common.h"

void pit_init()
{
	MCF_PIT0_PCSR &= ~(1); // disable timer
	
	MCF_PIT0_PCSR |= 1 << 5; // DEBUG MODE ENABLED
	MCF_PIT0_PCSR |= 1 << 4; // Immediately overwrite value in PIT counter
	MCF_PIT0_PCSR |= 1 << 3; // Enable Interrupts
	MCF_PIT0_PCSR |= 1 << 2; // Clear interrupt request flag
	MCF_PIT0_PCSR |= 1 << 1; // Make PCNTR0 value into PMR0
	
	
	
	
	set_note_duration(0,0);
	//MCF_INTC0_ICR55 = 1; // NOT SURE ON #9
	
	set_pit_handler(64+55, pit_handler);
	
	MCF_PIT0_PCSR |= 1; // Enable timer
}



/*
void set_pit_handler(int vector, void (*handler)(void))
{
	extern uint32 __VECTOR_RAM[];
	__VECTOR_RAM[vector]=(uint32)handler;
	
	// Unmask the 55th slot
	MCF_INTC0_IMRH &= ~(1) << 23;
	//set PIT's (55) interrupt level to 5 and priority to 7
	MCF_INTC0_ICR55 |= (5) << 3;
	MCF_INTC0_ICR55 |= (7);
}


__declspec(interrupt) void pit_handler(void)
{
	
	//write to PIF to clear interrupt flag
	//may need to call a function instead to check what length is next note and write to PMR to set the new counter
	MCF_PIT0_PCSR |= 1 << 2;
	/*OR 
	 * set counter to change the length of next note(or pause)
	 * 
	 *
	//Play the next note in the sequence	
	play_next();
}
*/

